package qiniu.voidcepc.voicepc.demos.web.utils;

import java.awt.*;

public class MouseCoordinateHelper {
    /**
     * 将物理坐标转换为逻辑坐标（考虑DPI缩放）
     */
    public static Point physicalToLogical(int physicalX, int physicalY) {
        Dimension logicalSize = Toolkit.getDefaultToolkit().getScreenSize();
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice();
        DisplayMode dm = gd.getDisplayMode();

        double scaleX = (double)dm.getWidth() / logicalSize.width;
        double scaleY = (double)dm.getHeight() / logicalSize.height;

        return new Point((int)(physicalX / scaleX), (int)(physicalY / scaleY));
    }

    /**
     * 将逻辑坐标转换为物理坐标
     */
    public static Point logicalToPhysical(int logicalX, int logicalY) {
        Dimension logicalSize = Toolkit.getDefaultToolkit().getScreenSize();
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice();
        DisplayMode dm = gd.getDisplayMode();

        double scaleX = (double)dm.getWidth() / logicalSize.width;
        double scaleY = (double)dm.getHeight() / logicalSize.height;

        return new Point((int)(logicalX * scaleX), (int)(logicalY * scaleY));
    }
}
